<?php

use Illuminate\Http\Request;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});

Route::group(['prefix' => 'common'], function(){
    Route::get('getImages', 'Api\BaseImgController@getImages');
});

Route::group(['prefix' => 'user'], function(){
    Route::post('sendverifycode', 'Api\UserController@getRegisterVerifyCode');
    Route::post('binduser', 'Api\UserController@bindUserWithMobileAndOpenId');
    Route::post('loginbyopenid', 'Api\UserController@loginWithOpenId');
    Route::post('getopenidbycode', 'Api\UserController@getUserOpenIdByAuthCode');
});

Route::group(['prefix' => 'activity'], function(){
    Route::post('getActivityList', 'Api\ActivityController@getActivityList');
});

Route::group(['prefix' => 'activity'], function(){
    Route::post('activityDetail', 'Api\ActivityController@getActivityDetail');
});

Route::group(['prefix' => 'news'], function(){
    Route::post('getNewsList', 'Api\NewsController@getNewsList');
});

Route::group(['prefix' => 'news'], function(){
    Route::post('getNewsDetail', 'Api\NewsController@getNewsDetail');
});

Route::group(['prefix' => 'Note'], function(){
    Route::get('getNotesList/{uid}', 'Api\NotesController@getNotesList');
});

Route::group(['prefix' => 'Note'], function(){
    Route::get('getNotesDetail/{id}', 'Api\NotesController@getNotesDetail');
});

Route::group(['prefix' => 'Group'], function(){
    Route::get('getGroupList/{cityId}', 'Api\UserGroupController@getGroupList');
});

Route::group(['prefix' => 'activity'], function(){
    Route::post('joinActivityApply', 'Api\ActivityController@joinActivityApply');
});
$routes = \App\Lib\Core\RouteBuilder::buildApiRoutes();
foreach ($routes as $route) {
    if ($route['method'] === \App\Route::$ROUTE_METHOD_ANY) {
        if (!empty($route['slug'])) {
            Route::any($route['route'], $route['action'])->middleware('can:'.$route['code']);
        } else {
            Route::any($route['route'], $route['action'])->name($route['slug'])->middleware('can:'.$route['code']);
        }
    } else if ($route['method'] === \App\Route::$ROUTE_METHOD_GET) {
        if (!empty($route['slug'])) {
            Route::get($route['route'], $route['action'])->middleware('can:'.$route['code']);
        } else {
            Route::get($route['route'], $route['action'])->name($route['slug'])->middleware('can:'.$route['code']);
        }
    } else if ($route['method'] === \App\Route::$ROUTE_METHOD_POST) {
        if (!empty($route['slug'])) {
            Route::post($route['route'], $route['action'])->middleware('can:'.$route['code']);
        } else {
            Route::post($route['route'], $route['action'])->name($route['slug'])->middleware('can:'.$route['code']);
        }
    } else if ($route['method'] === \App\Route::$ROUTE_METHOD_PUT) {
        if (!empty($route['slug'])) {
            Route::put($route['route'], $route['action'])->middleware('can:'.$route['code']);
        } else {
            Route::put($route['route'], $route['action'])->name($route['slug'])->middleware('can:'.$route['code']);
        }
    } else if ($route['method'] === \App\Route::$ROUTE_METHOD_DELETE) {
        if (!empty($route['slug'])) {
            Route::delete($route['route'], $route['action'])->middleware('can:'.$route['code']);
        } else {
            Route::delete($route['route'], $route['action'])->name($route['slug'])->middleware('can:'.$route['code']);
        }
    } else if ($route['method'] === \App\Route::$ROUTE_METHOD_PATCH) {
        if (!empty($route['slug'])) {
            Route::patch($route['route'], $route['action'])->middleware('can:'.$route['code']);
        } else {
            Route::patch($route['route'], $route['action'])->name($route['slug'])->middleware('can:'.$route['code']);
        }
    } else if ($route['method'] === \App\Route::$ROUTE_METHOD_OPTIONS) {
        if (!empty($route['slug'])) {
            Route::options($route['route'], $route['action'])->middleware('can:'.$route['code']);
        } else {
            Route::options($route['route'], $route['action'])->name($route['slug'])->middleware('can:'.$route['code']);
        }
    }
}
